
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOuresponseBo;
import com.wyzz.hr.dao.IOuresponseDao;
import com.wyzz.hr.pojo.Ouresponse;
import com.wyzz.hr.pojo.PositionBase;


public class OuresponseBoImpl implements IOuresponseBo {

	public OuresponseBoImpl() {
	}

	public IOuresponseDao getOuresponseDao() {
		return ouresponseDao;
	}

	public void setOuresponseDao(IOuresponseDao ouresponseDao) {
		this.ouresponseDao = ouresponseDao;
	}

	public Ouresponse saveDeptRes(Ouresponse deptRes) {
		if (StringUtils.isEmpty(deptRes.getId())) {
			if (getDeptRespByName(deptRes) != null) {
				deptRes.setInfo("EXISTED");
				return deptRes;
			}
			deptRes.setOurSortId(getMaxSortId());
			ouresponseDao.saveObject(deptRes);
		} else {
			ouresponseDao.updateObject(deptRes);
		}
		deptRes.setInfo("SUCC");
		return deptRes;
	}

	public boolean deleteDeptResById(String deptResId) {
		String hql = (new StringBuilder()).append(
				"delete from Ouresponse where id = '").append(deptResId)
				.append("'").toString();
		ouresponseDao.exeHql(hql);
		return true;
	}

	public boolean saveSortIdBatch(String ids[]) {
		int sortId = 1;
		for (int i = 0; ids.length > 0 && i < ids.length; i++) {
			ouresponseDao.exeHql((new StringBuilder()).append(
					"update Ouresponse set ourSortId=").append(sortId).append(
					" where id ='").append(ids[i]).append("'").toString());
			sortId++;
		}

		return true;
	}

	public Ouresponse getDeptRespByName(Ouresponse deptRes) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouresponse.class);
		dc.add(Restrictions.eq(Ouresponse.PROP_OUR_DEPT, deptRes.getOurDept()));
		dc.add(Restrictions.eq(Ouresponse.PROP_OUR_NAME, deptRes.getOurName()));
		List resList = ouresponseDao.findByCriteria(dc);
		if (resList.size() == 0)
			return null;
		else
			return (Ouresponse) resList.get(0);
	}

	public List getDeptRespLisByDept(String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouresponse.class);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Ouresponse.PROP_OUR_DEPT).append(".id").toString(), deptId));
		dc.addOrder(Order.asc(Ouresponse.PROP_OUR_SORT_ID));
		List resList = ouresponseDao.findByCriteria(dc);
		return resList;
	}

	public synchronized Integer getMaxSortId() {
		String hql = "select max(ourSortId) as maxSortId from Ouresponse res";
		List maxNoList = ouresponseDao.exeHqlList(hql);
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public List getPbRespByPbId(String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouresponse.class);
		dc.add(Restrictions.eq(Ouresponse.PROP_OUR_PB, new PositionBase(pbId)));
		dc.addOrder(Order.asc(Ouresponse.PROP_OUR_SORT_ID));
		return ouresponseDao.findByCriteria(dc);
	}

	public boolean addPbResp(Ouresponse pbResp) {
		boolean duplicateName = duplicateName(pbResp, "add");
		if (!duplicateName) {
			pbResp.setOurSortId(Integer.valueOf(0));
			ouresponseDao.saveObject(pbResp);
			return true;
		} else {
			return false;
		}
	}

	public boolean updatePbResp(Ouresponse pbResp) {
		boolean duplicateName = duplicateName(pbResp, "update");
		if (!duplicateName) {
			Ouresponse old = (Ouresponse) ouresponseDao.loadObject(
					com.wyzz.hr.pojo.Ouresponse.class, pbResp.getId(), null,
					new boolean[0]);
			old.setOurName(pbResp.getOurName());
			old.setOurDesc(pbResp.getOurDesc());
			old.setOurRate(pbResp.getOurRate());
			ouresponseDao.updateObject(old);
			return true;
		} else {
			return false;
		}
	}

	private boolean duplicateName(Ouresponse pbResp, String operate) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouresponse.class);
		dc.add(Restrictions.eq(Ouresponse.PROP_OUR_PB, new PositionBase(pbResp
				.getOurPb().getId())));
		dc.add(Restrictions.eq(Ouresponse.PROP_OUR_NAME, pbResp.getOurName()));
		if (operate.equals("update"))
			dc.add(Restrictions.not(Restrictions.eq(Ouresponse.PROP_ID, pbResp
					.getId().trim())));
		List pbRespByName = ouresponseDao.findByCriteria(dc);
		return pbRespByName != null && pbRespByName.size() != 0;
	}

	public boolean delPbResp(String respId) {
		try {
			ouresponseDao.deleteObject(com.wyzz.hr.pojo.Ouresponse.class,
					respId);
			return true;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void savePbRespOrder(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			ouresponseDao.exeHql((new StringBuilder()).append(
					"update Ouresponse set ourSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	private IOuresponseDao ouresponseDao;
}
